package ZorkObjects;

import java.util.HashMap;
import java.util.ArrayList;

public class Container extends ZorkObject
{
    private ArrayList<String> accepts;
    private ArrayList<Item> items;
    boolean opened;

    public Container(HashMap configMap, HashMap extraConfigMap)
    {
        super(configMap);

        ArrayList<String> containedItems = null;
        items = new ArrayList<Item>();
        Object containedItemObject = configMap.remove("item");
        opened = false;
        accepts = new ArrayList<String>();

        if(containedItemObject instanceof ArrayList) {
            containedItems = (ArrayList<String>)containedItemObject;
        }
        else if(containedItemObject instanceof String) {
            containedItems = new ArrayList<String>();
            containedItems.add((String)containedItemObject);
        }
        else if(containedItemObject == null) {
            return; 
        }

        ArrayList<HashMap> itemsMap = (ArrayList<HashMap>)extraConfigMap.get("item");

        for(HashMap h : itemsMap)
        {
            if(containedItems.contains(h.get("name"))) {
                items.add(new Item(h));
            }
        }

        Object acceptContainer = configMap.remove("accept");
        loadContainer(acceptContainer, accepts);
    }

    private void loadContainer(Object container, ArrayList target)
    {
        if(container != null)
        {
            if(container instanceof ArrayList)
            {
                for(String element : (ArrayList<String>)container)
                {
                    target.add(element);
                }
            }
            else if(container instanceof String) 
            {
                target.add((String)container);
            }
            else
            {
                System.out.println("this should never happen container");
                System.out.println(container);
                //System.out.println(container.getClass().toString());
            }
        }
    }

    public void setOpened() {
        opened = true;
    }

    public void setClosed() {
        opened = false;
    }
    
    public boolean isOpened() {
        return opened;
    }

    public boolean isClosed() {
        return !opened;
    }

    public void addItem(Item newItem) {
        items.add(newItem);
    }

	public ArrayList<Item> getItems() {
		return items;
	}

	public void removeItem(Item toRemove)
    {
        if(items.contains(toRemove)) {
            items.remove(toRemove);
        }
	}

    public boolean containsItem(String itemName)
    {
        for(Item i : items)
        {
            if(i.getName().equals(itemName)) {
                return true;
            }
        }

        return false;
    }

    public boolean containsItem(Item item)
    {
        if(items.contains(item)) {
            return true;
        }

        return false;
    }

    //For debugging
    public String toString()
    {
        String base = super.toString();

        for(Item i : items) {
            base += "item=\n";
            base += i.toString();
        }
        for(String s : accepts) {
            base += "accept = ";
            base += s;
            base += "\n";
        }

        return base;
    }
}
